﻿using System.Collections.Generic;
using Wedn.Core.Entity;

namespace Wedn.Core.Data
{
    /// <summary>
    /// 关系管理类
    /// </summary>
    public static class RelationManager
    {
        private static Wedn.Interface.DAL.IRelation dao;
        /// <summary>
        /// 链接管理类构造函数
        /// </summary>
        /// <exception cref="未找到系统数据库访问组件"></exception>
        static RelationManager()
        {
            dao = Wedn.Interface.DAOFactory.CreateRelationDAO();
            if (dao == null)
            {
                throw new System.Exception("缺失数据库访问组件!");
            }
        }
        /// <summary>
        /// 向关系表中插入一行新数据, 返回执行结果受影响行数
        /// </summary>
        /// <param name="relation">要插入的对象</param>
        /// <returns>执行结果受影响行数</returns>
        public static int Insert(Relation relation)
        {
            return dao.Insert(relation);
        }

        /// <summary>
        /// 根据关系ID删除对应的关系记录, 返回执行结果受影响行数
        /// </summary>
        /// <param name="id">要删除记录的ID</param>
        /// <returns>执行结果受影响行数</returns>
        public static int Delete(int id)
        {
            return dao.Delete(id);
        }

        /// <summary>
        /// 根据关系ID数组删除对应的关系记录, 返回执行结果受影响行数
        /// </summary>
        /// <param name="ids">要删除记录的ID数组</param>
        /// <returns>执行结果受影响行数</returns>
        public static int Delete(int[] ids)
        {
            return dao.Delete(ids);
        }

        /// <summary>
        /// 根据关系对象删除一个其所有关系, 返回执行结果受影响行数
        /// </summary>
        /// <param name="objectId">关系对象ID</param>
        /// <param name="objectType">关系对象类型</param>
        /// <returns>执行结果受影响行数</returns>
        public static int Delete(int objectId, string objectType)
        {
            return dao.Delete(objectId, objectType);
        }

        /// <summary>
        /// 更新一个完整的关系对象, 返回执行结果受影响行数
        /// </summary>
        /// <param name="relation">要更新的对象</param>
        /// <returns>执行结果受影响行数</returns>
        public static int UpdateRelation(Relation relation)
        {
            return dao.UpdateRelation(relation);
        }

        /// <summary>
        /// 更新一个关系信息, 返回执行结果受影响行数
        /// </summary>
        /// <param name="objectId">关系对象ID</param>
        /// <param name="objectType">关系对象类型</param>
        /// <param name="oldTermId">原先的标签ID</param>
        /// <param name="newTermId">更新的标签ID</param>
        /// <returns>执行结果受影响行数</returns>
        public static int UpdateInfo(int objectId, string objectType, int oldTermId, int newTermId)
        {
            return dao.UpdateInfo(objectId, objectType, oldTermId, newTermId);
        }

        /// <summary>
        /// 将一个标签ID全部更新为新的标签ID, 返回执行结果受影响行数
        /// </summary>
        /// <param name="oldTermId">原先的标签ID</param>
        /// <param name="newTermId">更新的标签ID</param>
        /// <returns>执行结果受影响行数</returns>
        public static int UpdateTermId(int oldTermId, int newTermId)
        {
            return dao.UpdateTermId(oldTermId, newTermId);
        }

        /// <summary>
        /// 获取表中主键最大值, 返回主键最大值
        /// </summary>
        /// <returns>主键最大值</returns>
        public static int GetMaxId()
        {
            return dao.SelectMaxId();
        }

        /// <summary>
        /// 根据标签ID和对象类型取出当前关系表中对应的关系对象ID
        /// </summary>
        /// <param name="objectType">关系对象类型</param>
        /// <param name="termId">标签ID</param>
        /// <returns>指定标签指定类型的全部关系对象ID数组</returns>
        public static int[] GetObjectId(string objectType, int termId)
        {
            return dao.SelectObjectId(objectType, termId);
        }

        /// <summary>
        /// 根据对象ID和对象类型取出当前关系表中对应的标签ID
        /// </summary>
        /// <param name="objectId">关系对象ID</param>
        /// <param name="objectType">关系对象类型</param>
        /// <returns>指定对象的全部标签ID数组</returns>
        public static int[] GetTermId(int objectId, string objectType)
        {
            return dao.SelectTermId(objectId, objectType);
        }

        /// <summary>
        /// 取出当前关系表中的所有关系信息集合
        /// </summary>
        /// <returns>所有关系信息集合</returns>
        public static List<Relation> GetRelationList()
        {
            return dao.SelectRelationList();
        }

        /// <summary>
        /// 根据对象ID和对象类型取出当前关系表中的所有关系信息集合
        /// </summary>
        /// <param name="objectId">关系对象ID</param>
        /// <param name="objectType">关系对象类型</param>
        /// <returns>包涵指定对象关系信息集合</returns>
        public static List<Relation> GetRelationList(int objectId, string objectType)
        {
            return dao.SelectRelationList(objectId, objectType);
        }

        /// <summary>
        /// 根据关系ID取出当前关系表中的关系信息对象
        /// </summary>
        /// <param name="id">关系ID</param>
        /// <returns>关系信息对象</returns>
        public static Relation GetRelation(int id)
        {
            return dao.SelectRelation(id);
        }

        public static int GetTermCount(int tagid)
        {
            throw new System.NotImplementedException();
        }
    }
}
